package com.code_g.simple;

import com.code_g.utils.ConnectUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(Producer.QUEUE_NAME,true,false,false,null);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("路由key为: "+envelope.getRoutingKey());
                System.out.println("交换机为: "+envelope.getExchange());
                System.out.println("消息id为: "+envelope.getDeliveryTag());
                System.out.println("接受的信息为: "+new String(body, StandardCharsets.UTF_8));
            }
        };

        //1.队列名
        //2.是否自动接受消息
        //3.消费者
        channel.basicConsume(Producer.QUEUE_NAME,true,defaultConsumer);
    }

}
